<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends Www_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('attachs_m');
    }

    /**
     * 轮播图
     */
    public function menu()
    {
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc'] = $this->banner_m->select(['type' => 0]);

        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        $data['banner'] = $banner;

        $this->load->view('menu', $data);
    }

    /**
     * 首页
     */
    public function index()
    {
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc'] = $this->banner_m->select(['type' => 0]);

        $banner['mobile'] = $this->banner_m->select(['type' => 1]);

        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        if ($banner['mobile']) {
            foreach ($banner['mobile'] as $key => $val) {
                $banner['mobile'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['mobile'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['mobile'][$key]['pic_url']);
            }
        }

        // 特色物业
        $this->load->model('properties_m');
        $hot = $this->properties_m->select('', 3, 0, "id,title,image_access_key,describe");
        // 图片
        if (!empty($hot)) {
            foreach ($hot as $key => $val) {
                $access_key       = $val['image_access_key'];
                $imgs             = $this->attachs_m->get_one_attachs_by($access_key);
                $hot[$key]['img'] = base_url('uploads/wuye/' . $access_key . "/" . $imgs['file_name']);
            }
        }

        // 最近交易
        $product = $this->properties_m->select("", 6, 0, "id,title,price,image_access_key");
        // 图片
        if (!empty($product)) {
            foreach ($product as $key => $val) {
                $access_key           = $val['image_access_key'];
                $imgs                 = $this->attachs_m->get_one_attachs_by($access_key);
                $product[$key]['img'] = base_url('uploads/wuye/' . $access_key . "/" . $imgs['file_name']);
            }
        }
        //关于
        $this->load->model('page_m');
        $page = $this->page_m->find(1);

        $data['page'] = strip_tags($page['content']) ?? "";

        // 新闻
        $this->load->model('account_m');
        $news            = $this->account_m->select("", 4, 0, "id,title,img,describe,create_time");
        $data['news']    = $news;
        $data['product'] = $product;
        $data['hot']     = $hot;
        $data['banner']  = $banner;
        //pp($data);die;
        $this->load->view('index', $data);
    }

    /**
     * 关于我们
     */
    public function about()
    {
        $get  = $this->input->get();
        $page = $get['per_page'] ?? 1;
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc'] = $this->banner_m->select(['type' => 0]);

        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        $data['banner'] = $banner;
        $this->load->model('customer_m');
        // 个人
        $limit                       = 8;
        $config['use_page_numbers']  = true;
        $config['page_query_string'] = true;
        $config['total_rows']        = $this->customer_m->total();
        $config['base_url']          = base_url("about?");;
        $config['per_page']  = $limit;
        $config['num_links'] = 1;
        $start               = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        $hot              = $this->customer_m->select("", $limit, $start);
        $data['customer'] = $hot;
        //多单元建筑组合
        $this->load->model('page_m');
        $page         = $this->page_m->find(3);
        $data['page'] = $page ?? "";
        //多零售购物中心中心组合
        $page          = $this->page_m->find(2);
        $data['page2'] = $page ?? "";
        // pp($data);die;
        $this->load->view('about', $data);
    }

    /**
     * 个人详情
     */
    public function about_show($id = 0)
    {
        //个人资料
        $this->load->model('customer_m');
        $article          = $this->customer_m->find($id);
        $data["customer"] = $article ?? [];

        //物业
        $this->load->model('properties_m');
        $where      = "find_in_set({$id},`customer_id`) > 0";
        $properties = $this->properties_m->select($where, 10, 0, "id,title,image_access_key,describe,address,area,price,rate,space,customer_id");
        if (!empty($properties)) {
            foreach ($properties as $key => $val) {
                $access_key              = $val['image_access_key'];
                $imgs                    = $this->attachs_m->get_one_attachs_by($access_key);
                $properties[$key]['img'] = base_url('uploads/wuye/' . $access_key . "/" . $imgs['file_name']);
            }
        }
        $data['products'] = $properties ?? "";

        // pp($data);exit();
        $this->load->view('about_show', $data);
    }

    /**
     * 服务
     */
    public function service($id = 0)
    {
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc'] = $this->banner_m->select(['type' => 0]);

        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        $data['banner'] = $banner;

        //服务介绍
        $this->load->model('articleCategory_m');
        $this->load->model('article_m');

        $category = $this->articleCategory_m->get_all();
        if ($id) {
            $article = $this->article_m->findBy(['category' => $id]);
            $curCate = $this->articleCategory_m->find($id);
        } else {
            $article = $this->article_m->findBy(['category' => $category[0]['id']]);
            $curCate = $this->articleCategory_m->find($category[0]['id']);
        }
        $article['category_en_name'] = $curCate['category_en_name'];
        $article['category_name']    = $curCate['category_name'];

        $data["article"]  = $article ?? [];
        $data["category"] = $category ?? [];
        // pp($data);exit();
        $this->load->view('service', $data);
    }

    /**
     * 物业
     */
    public function properties()
    {
        $this->load->model('properties_m');
        $this->load->model('attachs_m');

        $get   = $this->input->get();
        $page  = $get['per_page'] ?? 1;
        $where = "type = 1 ";//查询全部就为$where = "1 = 1 "

        //根据物业类型搜索1,2,3,4,5
        // 1,4
        if (!empty($get['category'])) {
            $strToArr = explode(",", $get['category']);
            if (is_array($strToArr)) {
                $get['category'] = $strToArr;
                foreach ($strToArr as $k => $v) {
                    if (!empty($v)) {
                        $where .= " AND category like '%{$v}%'";
                    }
                }
            }
        }
        //根据建筑面积搜索
        if (!empty($get['area'])) {
            $strToArr         = explode(",", $get['area']);
            $get['areaStart'] = $strToArr[0];
            $get['areaEnd']   = $strToArr[1];
            $where            .= " AND area between {$strToArr[0]} and {$strToArr[1]}";
        }
        //根据价格范围搜索
        if (!empty($get['price'])) {
            $strToArr          = explode(",", $get['price']);
            $get['priceStart'] = $strToArr[0];
            $get['priceEnd']   = $strToArr[1];
            $where             .= " AND price between {$strToArr[0]} and {$strToArr[1]}";
        }

        //物业
        if (!empty($get['real'])) {
            $where .= " AND real = {$get['real']}";
        }
        //拍卖
        if (!empty($get['auction'])) {
            $where .= " AND auction = {$get['auction']}";
        }
        //dis
        if (!empty($get['distressed'])) {
            $where .= " AND distressed = {$get['distressed']}";
        }
        // 空间
        if (!empty($get['space'])) {
            $strToArr          = explode(",", $get['space']);
            $get['spaceStart'] = $strToArr[0];
            $get['spaceEnd']   = $strToArr[1];
            $where             .= " AND space between {$strToArr[0]} and {$strToArr[1]}";
        }

        // 费率
        if (!empty($get['rate'])) {
            $strToArr         = explode(",", $get['rate']);
            $get['rateStart'] = $strToArr[0];
            $get['rateEnd']   = $strToArr[1];
            $where            .= " AND rate between {$strToArr[0]} and {$strToArr[1]}";
        }

        //显示转租
        if (!empty($get['option'])) {
            $where .= " AND option = {$get['option']}";
        }
        //
        if (!empty($get['status'])) {
            $where .= " AND status = {$get['status']}";
        }
        //根据地址搜索
        if (!empty($get['keyword'])) {
            $where .= " AND (address like '%{$get['keyword']}%' or describe like '%{$get['keyword']}%')";
        }

        $orderBy = "id";
        $sort    = "desc";
        if (!empty($get['order'])) {
            $strToArr = explode("-", $get['order']);
            $orderBy  = $strToArr[0];
            $sort     = $strToArr[1];
        }

        // pp($where);
        // exit();
        //分页
        $limit                       = 4;
        $config['uri_segment']       = 3;
        $config['use_page_numbers']  = true;
        $config['page_query_string'] = true;
        $config['total_rows']        = $this->properties_m->total($where);
        $config['base_url']          = base_url('properties?') . http_build_query($get);;
        $config['per_page']  = $limit;
        $config['num_links'] = 1;
        $start               = ($page - 1) * $limit;
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();
        $products           = $this->properties_m->select_by($where, $limit, $start, "*", $orderBy, $sort);

        // 图片
        if (!empty($products)) {
            foreach ($products as $key => $val) {
                $access_key            = $val['image_access_key'];
                $imgs                  = $this->attachs_m->get_one_attachs_by($access_key);
                $products[$key]['img'] = base_url('uploads/wuye/' . $access_key . "/" . $imgs['file_name']);
            }
        }
        // 所有的类型
        $this->load->model('propertiesCategory_m');
        $data['propertiesCategory'] = $this->propertiesCategory_m->select(['deleted' => 0]);
        $data["products"]           = $products ?? [];
        $data["search"]             = $get;
        // pp( $data["search"]);exit();
        $this->load->view('properties', $data);
    }

    /**
     * 物业详情-总览
     */
    public function properties_show($id = 0)
    {
        $this->load->model('properties_m');

        $properties = $this->properties_m->find($id);

        // 图片
        $access_key  = $properties['image_access_key'];
        $imgs        = $this->attachs_m->get_attachs_by_access($access_key);
        $data['img'] = [];
        if (!empty($imgs)) {
            foreach ($imgs as $key => $val) {
                $data['img'][] = base_url('uploads/wuye/' . $access_key . "/" . $val['file_name']);
            }
        }
        $data["properties"] = $properties ?? [];

        // 个人
        $this->load->model('customer_m');
        if (!empty($properties['customer_id'])) {
            $customer = $this->customer_m->select("id in ({$properties['customer_id']})");
        } else {
            $customer = "";
        }
        $data["customer"] = $customer ?? [];

        $this->load->view('properties_show', $data);
    }

    /**
     * 物业详情-文件资料
     */
    public function properties_show2($id = 0)
    {
        $this->load->model('properties_m');
        $properties = $this->properties_m->find($id);
        // 图片
        $access_key  = $properties['image_access_key'];
        $imgs        = $this->attachs_m->get_attachs_by_access($access_key);
        $data['img'] = [];
        if (!empty($imgs)) {
            foreach ($imgs as $key => $val) {
                $data['img'][] = base_url('uploads/wuye/' . $access_key . "/" . $val['file_name']);
            }
        }
        $data["properties"] = $properties ?? [];

        // 个人
        $this->load->model('customer_m');
        $customer         = $this->customer_m->select('', 2, 0, "*");
        $data["customer"] = $customer ?? [];
        // pp($data);exit();
        $this->load->view('properties_show2', $data);
    }

    /**
     * 物业详情-照片
     */
    public function properties_show3($id = 0)
    {
        $this->load->model('properties_m');
        $properties = $this->properties_m->find($id);
        // 图片
        $access_key  = $properties['image_access_key'];
        $imgs        = $this->attachs_m->get_attachs_by_access($access_key);
        $data['img'] = [];
        if (!empty($imgs)) {
            foreach ($imgs as $key => $val) {
                $data['img'][] = base_url('uploads/wuye/' . $access_key . "/" . $val['file_name']);
            }
        }
        $data["properties"] = $properties ?? [];
        // 个人
        $this->load->model('customer_m');
        $customer         = $this->customer_m->select('', 2, 0, "*");
        $data["customer"] = $customer ?? [];
        // pp($data);exit();
        $this->load->view('properties_show3', $data);
    }

    /**
     * 物业详情-查看地图
     */
    public function properties_show4($id = 0)
    {
        $this->load->model('properties_m');
        $properties = $this->properties_m->find($id);
        // 图片
        $access_key  = $properties['image_access_key'];
        $imgs        = $this->attachs_m->get_attachs_by_access($access_key);
        $data['img'] = [];
        if (!empty($imgs)) {
            foreach ($imgs as $key => $val) {
                $data['img'][] = base_url('uploads/wuye/' . $access_key . "/" . $val['file_name']);
            }
        }
        if(empty($properties['longitude'])){
            $properties['longitude'] = "116.3";
        }
        if(empty($properties['dimension'])){
            $properties['dimension'] = "39.9";
        }
        $data["properties"] = $properties ?? [];
        // 个人
        $this->load->model('customer_m');
        $customer         = $this->customer_m->select('', 2, 0, "*");
        $data["customer"] = $customer ?? [];
        // pp($data);exit();
        $this->load->view('properties_show4', $data);
    }

    /**
     * 住宅
     */
    public function press()
    {
        $this->load->model('properties_m');
        $this->load->model('attachs_m');

        $get   = $this->input->get();
        $page  = $get['per_page'] ?? 1;
        $where = "type= 2";
        //根据地址搜索
        if (!empty($get['keyword'])) {
            $where .= " AND (address like '%{$get['keyword']}%' or describe like '%{$get['keyword']}%')";
        }

        $orderBy = "id";
        $sort    = "desc";
        if (!empty($get['order'])) {
            $strToArr = explode("-", $get['order']);
            $orderBy  = $strToArr[0];
            $sort     = $strToArr[1];
        }

        // pp($where);
        // exit();
        //分页
        $limit                       = 5;
        $config['uri_segment']       = 3;
        $config['use_page_numbers']  = true;
        $config['page_query_string'] = true;
        $config['total_rows']        = $this->properties_m->total($where);
        $config['base_url']          = base_url('properties?') . http_build_query($get);;
        $config['per_page']  = $limit;
        $config['num_links'] = 1;
        $start               = ($page - 1) * $limit;
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();
        $products           = $this->properties_m->select_by($where, $limit, $start, "*", $orderBy, $sort);

        // 图片
        if (!empty($products)) {
            foreach ($products as $key => $val) {
                $access_key            = $val['image_access_key'];
                $imgs                  = $this->attachs_m->get_one_attachs_by($access_key);
                $products[$key]['img'] = base_url('uploads/wuye/' . $access_key . "/" . $imgs['file_name']);
            }
        }

        $data["products"] = $products ?? [];
        $data["search"]   = $get;
        // pp( $data["search"]);exit();
        $this->load->view('press', $data);
    }

    /**
     * 住宅详情
     */
    public function press_show($id = 0)
    {
        $this->load->model('properties_m');
        $properties = $this->properties_m->find($id);
        // 图片
        $access_key  = $properties['image_access_key'];
        $imgs        = $this->attachs_m->get_attachs_by_access($access_key);
        $data['img'] = [];
        if (!empty($imgs)) {
            foreach ($imgs as $key => $val) {
                $data['img'][] = base_url('uploads/wuye/' . $access_key . "/" . $val['file_name']);
            }
        }
        $data["properties"] = $properties ?? [];
        // 个人
        $this->load->model('customer_m');
        $customer         = $this->customer_m->select('', 2, 0, "*");
        $data["customer"] = $customer ?? [];
        $this->load->view('press_show', $data);
    }

    /**
     * 联系我们
     */
    public function contact()
    {
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc'] = $this->banner_m->select(['type' => 0]);

        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        $data['banner'] = $banner;
        $data['rid']    = 5;
        $this->load->view('contact', $data);
    }

    /**
     * 加入我们
     */
    public function join()
    {
        $data['rid'] = 6;
        $this->load->view('join', $data);
    }

    /**
     * 获奖
     */
    public function news()
    {
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc'] = $this->banner_m->select(['type' => 0]);

        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        $data['banner'] = $banner;
        // 获奖
        $this->load->model('page_m');
        $page         = $this->page_m->find(4);
        $data['page'] = $page ?? "";

        $this->load->view('news', $data);
    }

    /**
     * 新闻发布列表
     */
    public function news_list($id = 0, $page = 1)
    {
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc'] = $this->banner_m->select(['type' => 0]);

        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        $data['banner'] = $banner;

        // 获奖
        $where = ['deletetd' => 0];
        $this->load->model('account_m');
        $this->load->library('pagination');
        $limit                      = 2;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->account_m->total($where);
        $config['base_url']         = base_url('news_list/' . $id . "/");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $field              = "id,title,describe,create_time";
        $data["news"]       = $this->account_m->select($where, $limit, $start, $field);
        $data["total"]      = $config['total_rows'];

        $this->load->view('news_list', $data);
    }

    /**
     * 新闻详情
     */
    public function news_show($id = 0)
    {
        // 轮播图
        $this->load->model('banner_m');
        $banner['pc'] = $this->banner_m->select(['type' => 0]);

        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }

        $data['banner'] = $banner;

        $this->load->model('account_m');
        $article         = $this->account_m->find($id);
        $data["article"] = $article ?? [];

        //获取上一篇
        // $data["pre_article"]  = $this->account_m->findBy("id < {$id}");
        // $data['next_article'] = $this->account_m->findBy("id > {$id}");
        // pp($data);exit();
        $this->load->view('news_show', $data);
    }

    /**
     * 留言提交
     */
    public function message_add()
    {

        $post  = $this->input->post();
        $title = isset($post['name']) ? htmlspecialchars($post['name'], ENT_QUOTES) : false;
        $tel   = isset($post['tel']) ? htmlspecialchars($post['tel'], ENT_QUOTES) : false;
        $email = isset($post['email']) ? htmlspecialchars($post['email'], ENT_QUOTES) : false;
        $con   = isset($post['con']) ? htmlspecialchars($post['con'], ENT_QUOTES) : false;

        if (!$title) {
            echo json_encode(['status' => 0, 'msg' => '姓名不能为空']);
            exit();
        }
        if (!$tel) {
            echo json_encode(['status' => 0, 'msg' => '电话不能为空']);
            exit();
        }
        if (!$email) {
            echo json_encode(['status' => 0, 'msg' => '邮箱不能为空']);
            exit();
        }
        if (!$con) {
            echo json_encode(['status' => 0, 'msg' => '备注不能为空']);
            exit();
        }
        $properties = [
            'name'  => $title,
            'tel'   => $tel,
            'email' => $email,
            'con'   => $con,
            'time'  => date("Y-m-d H:i:s", time()),
        ];
        // 新增
        $this->load->model('message_m');

        $properties = $this->message_m->insert($properties);
        if ($properties) {
            echo json_encode(['status' => 1, 'msg' => '留言成功']);
            exit();
        } else {
            echo json_encode(['status' => 0, 'msg' => '留言失败']);
            exit();
        }
    }

    /**
     * 邮件订阅
     */
    public function email_add()
    {

        $post  = $this->input->post();
        $email = isset($post['email']) ? htmlspecialchars($post['email'], ENT_QUOTES) : false;
        if (!$email) {
            echo json_encode(['status' => 0, 'msg' => '订阅邮箱不能为空']);
            exit();
        }
        $properties = [
            'email' => $email,
            'time'  => date("Y-m-d H:i:s", time()),
        ];

        // 新增
        $this->load->model('subscribe_m');
        $properties = $this->subscribe_m->insert($properties);
        if ($properties) {
            echo json_encode(['status' => 1, 'msg' => '订阅成功']);
            exit();
        } else {
            echo json_encode(['status' => 0, 'msg' => '订阅失败']);
            exit();
        }
    }

    // 所有404頁面進入這裡
    public function not_found()
    {

    }

}
